#!/bin/sh
# This regression test is a part of SIPp.
# pcap plays with no raw socket to send them from, as SIPp may not open
# one: SIPp exits with 255 on the error, and does not hang.
. "`dirname "$0"`/../functions"

# Run as root in a user and network namespace of our own, with no
# CAP_NET_RAW.
if test -z "$SIPP_NETNS"; then
    if unshare -rn true 2>/dev/null; then
        SIPP_NETNS=1 exec unshare -rn "$0"
    fi
    init
    skip "no user network namespaces"
fi
init
"`get_sipp`" -v | grep -q PCAP || skip "no PCAP"
ip link set lo up 2>/dev/null || skip "cannot bring up lo"
setpriv --bounding-set -net_raw true 2>/dev/null || skip "no setpriv"

# The call of github-#0039, which plays audio and video at once, with
# its plays each a hundred times over: the call still starts plays when
# the playback thread would fail to open the socket.
uac=`mktemp`
awk '/<exec play_pcap/ { for (i = 0; i < 100; i++) print } { print }' \
    ../github-#0039/uac.xml >$uac
sippbg -sf ../github-#0039/uas.xml -i 127.0.0.1 -p 25790 -m 1 -timeout 10
setpriv --bounding-set -net_raw timeout -sKILL 10 "`get_sipp`" -nostdin \
    -sf $uac -i 127.0.0.1 -p 25791 -mp 26000 -m 1 127.0.0.1:25790 \
    -timeout 5 >/dev/null 2>uac.log
status=$?
rm -f $uac

if test $status -ne 255; then
    fail "exited with $status"
elif ! grep -q "Can't create raw IPv4 socket" uac.log; then
    fail "no error on the raw socket"
else
    rm -f uac.log
    ok
fi
